﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
namespace LinkCard.AdminCP.AboutUs
{
    public partial class aboutus_new : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!IsPostBack)
            {
                DataTable dt;
                // Category Option 
                dt = DataHelper.SelectTable("SELECT AboutUsCateID, Name FROM AboutUsCate ORDER BY Position", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "Name";
                CategoryID.DataValueField = "AboutUsCateID";
                CategoryID.DataBind();

                PostDate.Value = DateTime.Today.ToShortDateString();
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            add();
            Response.Redirect("/AboutUs/aboutus_all_published.aspx");
        }

        protected void add()
        {
            var dao = new AboutUsDao();
            var item = new LinkCard.Model.AboutUs();

            item.Title = AboutUsTitle.Value;
            item.PostDate = ConvertUtility.ToDateTime(PostDate.Value);
            item.Content = Content.Value;
            item.AboutUsCateID = ConvertUtility.ToInt64(CategoryID.SelectedValue);
            item.History = "Bài viết được tạo bởi " + Session["USER"].ToString() + " lúc " + DateTime.Now.ToString() + "";
            dao.Add(item);

        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
